package com.mb.app.controller;

import com.mb.app.domain.TbIndexClassCfg;
import com.mb.app.domain.TbIndexClassType;
import com.mb.app.service.ITbIndexClassCfgService;
import com.mb.app.service.ITbIndexClassTypeService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页类别配置  前端控制器
 * </p>
 *
 * @author zwl
 * @since 2019-04-03
 */
@Controller
@RequestMapping("/app/indexClassCfg")
@Slf4j
public class TbIndexClassCfgController extends BaseController {
    

    @Autowired
    private ITbIndexClassCfgService indexClassCfgService;

    @Autowired
    private ITbIndexClassTypeService indexClassTypeService;
    
    @GetMapping("/index/{classType}")
    String index(@PathVariable("classType") Integer classType,Model model){
        model.addAttribute("classType",classType);
        return "/app/indexClassCfg/indexClassCfg_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbIndexClassCfg> list = indexClassCfgService.queryList(query);
        int total = indexClassCfgService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add/{classType}")
    String add(@PathVariable("classType") String classType,Model model){
        // 根据classType 查询 首页分类
        TbIndexClassType indexClassType = indexClassTypeService.getDtl(Long.parseLong(classType));
        model.addAttribute("indexClassType",indexClassType);
        return "/app/indexClassCfg/indexClassCfg_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(TbIndexClassCfg TbIndexClassCfg, HttpServletRequest request){
        TbIndexClassCfg.setAddUserId(getUserId());
        int result = indexClassCfgService.save(TbIndexClassCfg);
        return R.ok();
    }


    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("indexClassId") Long indexClassId, HttpServletRequest request){
        Long delUserId = getUserId();
        int result = indexClassCfgService.remove(indexClassId,delUserId);
        return R.ok();
    }

    @GetMapping("/dtl/{indexClassId}")
    String dtl(@PathVariable("indexClassId") Long indexClassId, Model model){
        model.addAttribute("indexClassCfgInfo",indexClassCfgService.getDtl(indexClassId));
        return "/app/indexClassCfg/indexClassCfg_info";
    }


    @GetMapping("/edit/{indexClassId}")
    String edit(@PathVariable("indexClassId") Long indexClassId, Model model){
        model.addAttribute("indexClassCfgInfo",indexClassCfgService.getDtl(indexClassId));
        return "/app/indexClassCfg/indexClassCfg_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(TbIndexClassCfg TbIndexClassCfg, HttpServletRequest request){
        TbIndexClassCfg.setUpdateUserId(getUserId());
        int result = indexClassCfgService.update(TbIndexClassCfg);
        return R.ok();
    }

    /**
     * 物流快递名称快递代码是否存在
     */
    @PostMapping("/indexClassCfgNameExit")
    @ResponseBody
    boolean indexClassCfgsNameExit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !indexClassCfgService.indexClassCfgNameExit(params);
    }

}
